package classes.managers {
	import classes.buttons.BaseButton;
	import classes.controlers.Click_Gauge_Controler;
	import classes.global.Global_Properties;
	import classes.scenes.A_Scene;
	import classes.sounds.SoundCollection;
	import flash.display.MovieClip;
	import flash.events.Event;
	import flash.events.MouseEvent;
	import flash.events.TimerEvent;
	import flash.utils.Timer;
	
	/**
	 * ...
	 * @author Kihoonee
	 */
	public class Game2_Manager extends A_Scene {
		
		// 부모클래스
		//private var main:Main;
		//private var mc:MovieClip;
		
		//객체
		private var gauge:Click_Gauge_Controler;
		
		private var jjing:int = 0;
		private var achieveJuice:int = 0;
		private var achieveCoin:int = 0;
		private var carrotJuice:int = 0;
		private var bucketUnit:int = 0;
		
		private var endCheck:int = 0;
		
		
		public function Game2_Manager( _main:Main, _mc:MovieClip ) {
			super( _main, _mc );
			
			gauge = new Click_Gauge_Controler( this, mc.squeeze_mc.gauge_mc );
			
			addEvent();
		}
		
		public function init():void {
			carrotJuice = Global_Properties.carrotJuice;
			jjing = 0;
			achieveJuice = 0;
			achieveCoin = 0;
			bucketUnit = 0;
			endCheck = 0;
			
			mc.startCount_mc.visible = false;
			mc.feedBack_mc.visible = false;
			mc.squeeze_mc.visible = false;
			mc.bucket_mc.visible = true;
			mc.bucket_mc.gotoAndStop( 1 );
			mc.gauge_mc.gotoAndStop( Global_Properties.count );
			
			getCoinAni(0);
			numberBoard( Global_Properties.score, mc.score_mc );
			
			startAni();
		}
		
		private function startAni():void {
			SoundCollection.bucketSound();
			
			bucketUnit = carrotJuice;
			switch (bucketUnit) {
				case 1: mc.bucket_mc.gotoAndPlay( "한박스" );
				break;
				case 2: mc.bucket_mc.gotoAndPlay( "두박스" );
				break;
				case 3: mc.bucket_mc.gotoAndPlay( "세박스" );
				break;
				case 4: mc.bucket_mc.gotoAndPlay( "네박스" );
				break;
				case 5: mc.bucket_mc.gotoAndPlay( "다섯박스" );
				break;
				case 6: mc.bucket_mc.gotoAndPlay( "다섯박스" );
				break;
				default:
			}
			
			// 게이지 비우기.
			reversePlay( mc.gauge_mc );
		}
		
		// ani 끝.
		private function bucket_END(e:Event):void {
			endCheck++;
			if ( endCheck === 2 ) {
				var timer:Timer = new Timer( 500, 1 );
				timer.addEventListener(TimerEvent.TIMER_COMPLETE, function(e:TimerEvent):void {
					timer.removeEventListener(TimerEvent.TIMER_COMPLETE, arguments.callee );
					mc.startCount_mc.visible = true;
					mc.startCount_mc.gotoAndPlay(2);
					SoundCollection.goSound();
				});
				timer.start();
			}
		}
		
		private function startCount_END():void {
			mc.startCount_mc.visible = false;
			
			startGame();
		}
		
		//게임 스타트.
		private function startGame():void {
			mc.squeeze_mc.visible = true;
			mc.bucket_mc.visible = false;
			
			gauge.init();
		}
		
		public function getJuice( _cnt:int ):void {
			switch ( _cnt ) {
				case 1: achieveJuice = carrotJuice * .3;
				break;
				case 2: achieveJuice = carrotJuice * .4;
				break;
				case 3: achieveJuice = carrotJuice * .6;
				break;
				case 4: achieveJuice = carrotJuice * .8;
				break;
				case 5: achieveJuice = carrotJuice;
				break;
				case 6: achieveJuice = carrotJuice;
				break;
				case 7: achieveJuice = carrotJuice;
				break;
				case 8: achieveJuice = carrotJuice;
				break;
				case 9: achieveJuice = carrotJuice;
				break;
				case 10: achieveJuice = carrotJuice;
				break;
				default:
			}
			achieveCoin = _cnt;
			mc.squeeze_mc.cnt_txt.text = String( achieveCoin );
			
			SoundCollection.waterSound();
		}
		
		public function getScore():void {
			Global_Properties.score += 100;
			numberBoard( Global_Properties.score, mc.score_mc );
		}
		
		public function getCoinAni( _cnt:int ):void {
			var i:int
			var length:int = 10;
			for ( i = 1; i <= length; ++i ) {
				mc.squeeze_mc.coinAni_mc["coin" + i + "_mc"].visible = false;
			}
			
			mc.squeeze_mc.coinAni_mc.coin1_mc.visible = false;
			
			
			switch (_cnt) {
				case 1: mc.squeeze_mc.coinAni_mc.coin1_mc.visible = true; mc.squeeze_mc.coinAni_mc.coin1_mc.gotoAndPlay(1); jjing += 10;
				break;
				case 2: mc.squeeze_mc.coinAni_mc.coin2_mc.visible = true; mc.squeeze_mc.coinAni_mc.coin2_mc.gotoAndPlay(1); jjing += 20;
				break;
				case 3: mc.squeeze_mc.coinAni_mc.coin3_mc.visible = true; mc.squeeze_mc.coinAni_mc.coin3_mc.gotoAndPlay(1); jjing += 30;
				break;
				case 4: mc.squeeze_mc.coinAni_mc.coin4_mc.visible = true; mc.squeeze_mc.coinAni_mc.coin4_mc.gotoAndPlay(1); jjing += 50;
				break;
				case 5: mc.squeeze_mc.coinAni_mc.coin5_mc.visible = true; mc.squeeze_mc.coinAni_mc.coin5_mc.gotoAndPlay(1); jjing += 50;
				break;
				case 6: mc.squeeze_mc.coinAni_mc.coin6_mc.visible = true; mc.squeeze_mc.coinAni_mc.coin6_mc.gotoAndPlay(1); jjing += 50;
				break;
				case 7: mc.squeeze_mc.coinAni_mc.coin7_mc.visible = true; mc.squeeze_mc.coinAni_mc.coin7_mc.gotoAndPlay(1); jjing += 50;
				break;
				case 8: mc.squeeze_mc.coinAni_mc.coin8_mc.visible = true; mc.squeeze_mc.coinAni_mc.coin8_mc.gotoAndPlay(1); jjing += 60;
				break;
				case 9: mc.squeeze_mc.coinAni_mc.coin9_mc.visible = true; mc.squeeze_mc.coinAni_mc.coin9_mc.gotoAndPlay(1); jjing += 80;
				break;
				case 10: mc.squeeze_mc.coinAni_mc.coin10_mc.visible = true; mc.squeeze_mc.coinAni_mc.coin10_mc.gotoAndPlay(1); jjing += 100;
				break;
				default:
			}
			
			SoundCollection.coinDropSound();
		}
		
		private function reversePlay( _mc:MovieClip ):void {
			_mc.addEventListener(Event.ENTER_FRAME, function (e:Event):void {
				_mc.prevFrame();
				if ( _mc.currentFrame === 1 ) {
					_mc.removeEventListener(Event.ENTER_FRAME, arguments.callee );
					endCheck++;
					if ( endCheck === 2 ) {
						var timer:Timer = new Timer( 500, 1 );
						timer.addEventListener(TimerEvent.TIMER_COMPLETE, function(e:TimerEvent):void {
							timer.removeEventListener(TimerEvent.TIMER_COMPLETE, arguments.callee );
							mc.startCount_mc.visible = true;
							mc.startCount_mc.gotoAndPlay(2);
							SoundCollection.goSound();
						});
						timer.start();
					}
				}
			});
		}
		
		private function numberBoard( _cnt:Number, _mc:MovieClip ):void {
			var str:String;
			var num10000:String;
			var num1000:String;
			var num100:String;
			var num10:String;
			var num1:String;
			
			if ( _cnt === 0 ) {
				 _mc.num10000_mc.gotoAndStop(1);
				 _mc.num1000_mc.gotoAndStop(1);
				 _mc.num100_mc.gotoAndStop(1);
				 _mc.num10_mc.gotoAndStop(1);
				 _mc.num1_mc.gotoAndStop(1);
			}
			
			if ( _cnt < 10 ) {
				_mc.num1_mc.gotoAndStop( _cnt +1 );
			}else if ( _cnt >= 10 && _cnt < 100 ) {
				str = String( _cnt );
				num10 = str.substr(0, 1);
				num1 = str.substr(1, 1);
				
				if( Number(num10) === 0 ) _mc.num10_mc.gotoAndStop( 1 );
				else _mc.num10_mc.gotoAndStop( Number(num10)+1 );
				
				if( Number(num1) === 0 ) _mc.num1_mc.gotoAndStop( 1 );
				else _mc.num1_mc.gotoAndStop( Number(num1)+1 );
			}else if ( _cnt >= 100 && _cnt < 1000 ) {
				str = String( _cnt );
				num100 = str.substr(0, 1);
				num10 = str.substr(1, 1);
				num1 = str.substr(2, 1);
				
				if( Number(num100) === 0 ) _mc.num100_mc.gotoAndStop( 1 );
				else _mc.num100_mc.gotoAndStop( Number(num100) + 1 );
				
				if( Number(num10) === 0 ) _mc.num10_mc.gotoAndStop( 1 );
				else _mc.num10_mc.gotoAndStop( Number(num10)+1 );
				
				if( Number(num1) === 0 ) _mc.num1_mc.gotoAndStop( 1 );
				else _mc.num1_mc.gotoAndStop( Number(num1)+1 );
			}else if ( _cnt >= 1000 && _cnt < 10000 ) {
				str = String( _cnt );
				num1000 = str.substr(0, 1);
				num100 = str.substr(1, 1);
				num10 = str.substr(2, 1);
				num1 = str.substr(3, 1);
				
				if( Number(num1000) === 0 ) _mc.num1000_mc.gotoAndStop( 1 );
				else _mc.num1000_mc.gotoAndStop( Number(num1000) + 1 );
				
				if( Number(num100) === 0 ) _mc.num100_mc.gotoAndStop( 1 );
				else _mc.num100_mc.gotoAndStop( Number(num100) + 1 );
				
				if( Number(num10) === 0 ) _mc.num10_mc.gotoAndStop( 1 );
				else _mc.num10_mc.gotoAndStop( Number(num10)+1 );
				
				if( Number(num1) === 0 ) _mc.num1_mc.gotoAndStop( 1 );
				else _mc.num1_mc.gotoAndStop( Number(num1)+1 );
			}else if ( _cnt >= 10000 && _cnt < 100000 ) {
				str = String( _cnt );
				num10000 = str.substr(0, 1);
				num1000 = str.substr(1, 1);
				num100 = str.substr(2, 1);
				num10 = str.substr(3, 1);
				num1 = str.substr(4, 1);
				
				if( Number(num10000) === 0 ) _mc.num10000_mc.gotoAndStop( 1 );
				else _mc.num10000_mc.gotoAndStop( Number(num10000) + 1 );
				
				if( Number(num1000) === 0 ) _mc.num1000_mc.gotoAndStop( 1 );
				else _mc.num1000_mc.gotoAndStop( Number(num1000) + 1 );
				
				if( Number(num100) === 0 ) _mc.num100_mc.gotoAndStop( 1 );
				else _mc.num100_mc.gotoAndStop( Number(num100) + 1 );
				
				if( Number(num10) === 0 ) _mc.num10_mc.gotoAndStop( 1 );
				else _mc.num10_mc.gotoAndStop( Number(num10)+1 );
				
				if( Number(num1) === 0 ) _mc.num1_mc.gotoAndStop( 1 );
				else _mc.num1_mc.gotoAndStop( Number(num1)+1 );
			}
		}
		
		private function addEvent():void {
			mc.bucket_mc.addEventListener( "bucket_END", bucket_END ); //frame dispatch
			mc.startCount_mc.addEventListener( "startCount_END", function(e:Event):void {
				startCount_END();
					
				//테스트용 
				//Global_Properties.carrotJuice = 6;
				//nextGame();
			});
		}
		
		public function feedBack():void {
			mc.feedBack_mc.visible = true;
			if ( achieveCoin == 0 ) {
				mc.feedBack_mc.cnt_mc.gotoAndStop( 11 );
			}else {
				mc.feedBack_mc.cnt_mc.gotoAndStop( achieveCoin );
			}
			
			var timer:Timer = new Timer( 2000, 1 );
			timer.addEventListener(TimerEvent.TIMER_COMPLETE, function (e:TimerEvent):void {
				timer.removeEventListener(TimerEvent.TIMER_COMPLETE, arguments.callee );
				mc.feedBack_mc.visible = false;
				nextGame();
			});
			timer.start();
		}
		
		public function nextGame():void {
			Global_Properties.coin = jjing;
			
			SoundCollection.rewardSound();
			
			main.viewChange( "coinAni_mc" );
			main.getCoin( achieveCoin );
		}
		
		
		public function timeOver():void {
			
		}
	}
}